﻿using System;
using System.Collections.Generic;
using NaverOpenApiLibrary.Models;

namespace NaverOpenApiLibrary.Services
{
    /// <summary>
    /// KIN (Knowledge IN) Service
    /// For more info: http://dev.naver.com/openapi/apis/search/kin
    /// </summary>
    public class KinService : NaverApiService<Kin>
    {
        // url pattern: http://openapi.naver.com/search?key={0}&query={1}&display={2}&start={3}&target=kin&sort={4}
        private const string URL_KIN_SEARCH_SERVICE = "http://openapi.naver.com/search";

        private string _keyword;
        private int _display;
        private int _start;
        private string _sort;

        public enum Sort
        {
            Sim, Date, Count, Point
        }

        public KinService(string apiKey)
            : base(apiKey, URL_KIN_SEARCH_SERVICE)
        {
            // DO NOTHING...
        }

        public void GetKin(string keyword)
        {
            GetKin(keyword, 1, 10, Sort.Sim);
        }

        public void GetKin(string keyword, int start, int display, Sort sort)
        {
            this._keyword = Uri.EscapeUriString(keyword);
            this._start = start;
            this._display = display;
            switch (sort)
            {
                case Sort.Count:
                    this._sort = "count";
                    break;

                case Sort.Date:
                    this._sort = "date";
                    break;

                case Sort.Point:
                    this._sort = "point";
                    break;

                case Sort.Sim:
                default:
                    this._sort = "sim";
                    break;
            }

            DoApiService();
        }

        protected override IDictionary<string, string> GetApiUriParameters()
        {
            return new Dictionary<string, string>()
            {
                {"query", this._keyword},
                {"target", "kin"},
                {"start", this._start.ToString()},
                {"display", this._display.ToString()},
                {"sort", this._sort}
            };
        }

        protected override Kin Deserialize(string rawData)
        {
            return base.ParseXmlResult(rawData);
        }
    }
}
